package com.zhangmeng.online.exam.ui.components;

import com.zhangmeng.online.exam.ui.utils.ApiUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 判断题组件
 *
 * @author zm
 * @date 2025/3/4 15:06
 * @version: 1.0
 */
public class TrueFalseComponent extends VBox {

    private Button indexButton;

    private static final String TRUE_TEXT = "✔ 正确";
    private static final String FALSE_TEXT = "✘ 错误";

    private Map<String, Object> context = new HashMap<>();

    public Map<String, Object> getContext() {
        return context;
    }

    public void setContext(Map<String, Object> context) {
        this.context = context;
    }

    private String correctAnswer = null; // 预设正确答案

    private int number_of_responses = 0;//作答次数

    public int getNumber_of_responses() {
        return number_of_responses;
    }

    public Label getQuestionLabel() {
        return questionLabel;
    }

    private final SimpleStringProperty result_answer = new SimpleStringProperty(); // 最终答案

    private HBox hBox = new HBox();

    private Label questionLabel;


    public void setBackground() {
        this.indexButton.setStyle("-fx-background-color: #4698ed;");
    }

    public TrueFalseComponent(String question, List<Map<String, Object>> options) {

        this.setStyle("-fx-background-color: white; -fx-border-color: #eadcdc;" + "-fx-border-radius: 10px; -fx-border-width: 1px; -fx-border-style: solid;-fx-background-radius: 10px;");


        // 题目组件
        questionLabel = new Label(question);
        hBox.getChildren().add(questionLabel);
        this.getChildren().add(hBox);

        // 单选按钮组
        ToggleGroup answerGroup = new ToggleGroup();

        // 创建选项按钮
        for (Map<String, Object> option : options) {
            boolean isAnswer = (boolean) option.get("isAnswer");
            if (isAnswer) {
                RadioButton trueButton = createRadioButton(TRUE_TEXT, answerGroup,option);
                this.getChildren().add(trueButton);
            } else {
                RadioButton falseButton = createRadioButton(FALSE_TEXT, answerGroup,option);
                this.getChildren().add(falseButton);
            }
        }


        // 功能组件
//        Button submitBtn = new Button("提交答案");
        Label resultLabel = new Label();

        // 提交事件处理
//        submitBtn.setOnAction(e -> {
//            if (answerGroup.getSelectedToggle() == null) {
//                resultLabel.setText("⚠ 请先选择答案");
//                resultLabel.setStyle("-fx-text-fill: #e67e22;");
//                return;
//            }
//
//            boolean userAnswer = ((RadioButton) answerGroup.getSelectedToggle()).getText().contains("正确");
//            if (userAnswer == correctAnswer) {
//                resultLabel.setText("✅ 回答正确！");
//                resultLabel.setStyle("-fx-text-fill: #2ecc71;");
//            } else {
//                resultLabel.setText("❌ 正确答案：" + (correctAnswer ? "正确" : "错误"));
//                resultLabel.setStyle("-fx-text-fill: #e74c3c;");
//            }
//        });

        answerGroup.selectedToggleProperty().addListener((ov, old_value, new_value) -> {
            RadioButton selected = (RadioButton) new_value;
            if (selected != null) {

                Map<String, Object> option  = (Map<String, Object>) selected.getUserData();
                number_of_responses++;
                context.put("option_id", option.get("id"));
                result_answer.set(option.get("id").toString());
                //提交数据
                ApiUtils.submitAnswer(context);
                boolean isAnswer = (boolean) option.get("isAnswer");
                if (isAnswer){
                    resultLabel.setText("你的选择："+ TRUE_TEXT);
                }else {
                    resultLabel.setText("你的选择："+ FALSE_TEXT);
                }
                resultLabel.setStyle("-fx-text-fill: #2ecc71;");
                setBackground();
            } else {
                resultLabel.setText("⚠ 请先选择答案");
            }
        });

        // 布局容器
        this.setSpacing(10);
        this.setPadding(new Insets(15));
        this.hBox.getChildren().add(1, resultLabel);
    }

    // 创建带样式的单选按钮‌:ml-citation{ref="1,3" data="citationList"}
    private RadioButton createRadioButton(String text, ToggleGroup group,Map<String, Object> option) {
        RadioButton rb = new RadioButton(text);
        rb.setToggleGroup(group);
        rb.setStyle("-fx-font-size: 14px; -fx-padding: 5px;");
        rb.setUserData(option);
        return rb;
    }

    public Button getIndexButton() {
        return indexButton;
    }

    public void setIndexButton(Button indexButton) {
        this.indexButton = indexButton;
    }
}
